package com.hjwoo.app.views;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.FailureResultObject;
import com.hjwoo.app.been.OP;
import com.hjwoo.app.common.AppManager;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestMethod;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.utils.LoanAccessTokenIdKeeper;
import com.hjwoo.app.utils.LoanLog;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.utils.Utils;

import java.io.File;
import java.util.HashMap;

/**
 * Created by yangchuan on 2016/5/10.
 * 借款信息
 */


public class ImmediatelyActivity extends BaseAppCompatActivity implements RequestListener {

    public static final int REQUEST_CODE_01 = 1;
    public static final int REQUEST_CODE_02 = 2;
    public static final int REQUEST_CODE_03 = 4;
    public static final int REQUEST_CODE_04 = 5;
    public static final int REQUEST_CODE_05 = 6;
    public static final File FILE_SDCARD = Environment
            .getExternalStorageDirectory();
    public static final File FILE_LOCAL = new File(FILE_SDCARD, "hjwoo");
    public static final File FILE_PIC_SCREENSHOT = new File(FILE_LOCAL, "images");
    //获取Activity实例
    public static ImmediatelyActivity instance;

    private HashMap op = new HashMap();//需要上传的参数
    private HashMap hashMap;

    private Gson gson = new Gson();
    //订单号
    private String serialNumber;
    //是不是在首页传入 ；是不是点击的立即报单
    private boolean isHome,isEvaluate2;
    private RelativeLayout houseinformation_furnishings_rll;
    private EditText requestedAmount, loanDuration, firstMortgageAmount, secondMortgageAmount;
    private TextView immediate_pensonal_tv, mortgageType, fourMortgageAmount_tv, sevenMortgageAmount_tv, firstMortgageAmountProperty_tv, imgtxt, fullName_et, immediately_cellphone_et, immediately_time_txt, immediately_next;
    private LinearLayout firstMortgageAmount_ll, secondMortgageAmount_ll, firstMortgageAmountProperty_ll;
    private LinearLayout mortgageType_ll, fourMortgageAmount, sevenMortgageAmount;
    private RelativeLayout imm_jumptoleadsfra_ll;
    private String a, o, p, w, e;
    private String str, str1;


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            //先判断是哪个页面返回过来的
            switch (requestCode) {
                case REQUEST_CODE_01:
                    immediate_pensonal_tv.setText(data.getStringExtra("itemName"));
                    a = data.getStringExtra("itemName");
                    str1 = data.getStringExtra("itemName");
                    if (str1.equals("是")) {
                        fourMortgageAmount.setVisibility(View.VISIBLE);
                        sevenMortgageAmount.setVisibility(View.GONE);
                    } else if (str1.equals("否")) {
                        fourMortgageAmount.setVisibility(View.VISIBLE);
                        sevenMortgageAmount.setVisibility(View.VISIBLE);
                    }
                    break;
                case REQUEST_CODE_02:
                    mortgageType.setText(data.getStringExtra("itemName"));
                    str = data.getStringExtra("itemName");
                    o = data.getStringExtra("itemName");
                    //                    "一抵", "二抵", "一抵转单", "二抵转单"
                    //        firstMortgageAmount_ll,secondMortgageAmount_ll,firstMortgageAmountProperty_ll
                    if (str.equals("一抵")) {
                        firstMortgageAmount_ll.setVisibility(View.GONE);
                        secondMortgageAmount_ll.setVisibility(View.GONE);
                        firstMortgageAmountProperty_ll.setVisibility(View.GONE);
                    } else if (str.equals("二抵") || str.equals("一抵转单")) {
                        firstMortgageAmount_ll.setVisibility(View.VISIBLE);
                        secondMortgageAmount_ll.setVisibility(View.GONE);
                        firstMortgageAmountProperty_ll.setVisibility(View.VISIBLE);
                    } else {
                        firstMortgageAmount_ll.setVisibility(View.VISIBLE);
                        secondMortgageAmount_ll.setVisibility(View.VISIBLE);
                        firstMortgageAmountProperty_ll.setVisibility(View.VISIBLE);
                    }
                    break;
                case REQUEST_CODE_03:
                    firstMortgageAmountProperty_tv.setText(data.getStringExtra("itemName"));
                    p = data.getStringExtra("itemName");
                    break;
                case REQUEST_CODE_04:
                    fourMortgageAmount_tv.setText(data.getStringExtra("itemName"));
                    w = data.getStringExtra("itemName");
                    break;
                case REQUEST_CODE_05:
                    sevenMortgageAmount_tv.setText(data.getStringExtra("itemName"));
                    e = data.getStringExtra("itemName");
                    break;
            }
        }
    }


    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            int i = 0;
            if (immediately_cellphone_et.getText().toString().trim().length() > 0) {
                i++;
            }
            int result = 10 + (i * 5);
            immediately_time_txt.setText("保护期时长：" + result + "天");
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_immediately);
        instance = ImmediatelyActivity.this;
        serialNumber = getIntent().getStringExtra("serialNumber");
        isHome = getIntent().getBooleanExtra("isHome",false);
        isEvaluate2 = getIntent().getBooleanExtra("isEvaluate2",false);

        Utils.initTitle("立即报单", ImmediatelyActivity.this, true);
        houseinformation_furnishings_rll = (RelativeLayout) findViewById(R.id.houseinformation_furnishings_rll);
        immediate_pensonal_tv = (TextView) findViewById(R.id.immediate_pensonal_tv);
        a = immediate_pensonal_tv.getText().toString().trim();
        firstMortgageAmountProperty_tv = (TextView) findViewById(R.id.firstMortgageAmountProperty_tv);
        p = firstMortgageAmountProperty_tv.getText().toString().trim();
        fourMortgageAmount_tv = (TextView) findViewById(R.id.fourMortgageAmountProperty_tv);
        w = fourMortgageAmount_tv.getText().toString().trim();
        sevenMortgageAmount_tv = (TextView) findViewById(R.id.sevenMortgageAmountProperty_tv);
        e = sevenMortgageAmount_tv.getText().toString().trim();
        requestedAmount = (EditText) findViewById(R.id.requestedAmount_et);
        loanDuration = (EditText) findViewById(R.id.loanDuration_et);
        mortgageType = (TextView) findViewById(R.id.mortgageType_et);
        o = mortgageType.getText().toString().trim();
        fullName_et = (TextView) findViewById(R.id.imm_fullName_et);
        immediately_cellphone_et = (TextView) findViewById(R.id.immediately_cellphone_et);
        fourMortgageAmount = (LinearLayout) findViewById(R.id.fourMortgageAmount_ll);
        sevenMortgageAmount = (LinearLayout) findViewById(R.id.sevenMortgageAmount_ll);
        firstMortgageAmount = (EditText) findViewById(R.id.firstMortgageAmount_et);
        secondMortgageAmount = (EditText) findViewById(R.id.secondMortgageAmount_et);
        mortgageType_ll = (LinearLayout) findViewById(R.id.mortgageType_ll);
        imm_jumptoleadsfra_ll = (RelativeLayout) findViewById(R.id.imm_jumptoleadsfra_ll);
        firstMortgageAmount_ll = (LinearLayout) findViewById(R.id.firstMortgageAmount_ll);
        secondMortgageAmount_ll = (LinearLayout) findViewById(R.id.secondMortgageAmount_ll);
        firstMortgageAmountProperty_ll = (LinearLayout) findViewById(R.id.firstMortgageAmountProperty_ll);
        immediately_next = (TextView) findViewById(R.id.immediately_next);
        immediately_time_txt = (TextView) findViewById(R.id.immediately_time_txt);

        if (!isEvaluate2){//如果不是点击立即报单而进入 则将跳转到评房列表的布局设置成隐藏
            imm_jumptoleadsfra_ll.setVisibility(View.GONE);
            findViewById(R.id.imm_show_ll).setVisibility(View.GONE);
        }


//        设置保护期随手号的输入而显示
        immediately_cellphone_et.addTextChangedListener(watcher);

        imm_jumptoleadsfra_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MainActivity.currIndex = 1;
                MainActivity.showFragment();
                MainActivity.group.check(R.id.foot_bar_im);

                //关闭所有Activity
                AppManager.getAppManager().finishAllActivity();
            }
        });
        houseinformation_furnishings_rll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle b = new Bundle();
                b.putSerializable("ls", new String[]{
                        "是", "否"
                });
                Intent i = new Intent(ImmediatelyActivity.this, SelectListActivity.class);
                i.putExtras(b);
                i.putExtra("equalStr", a);
                startActivityForResult(i, REQUEST_CODE_01);
            }
        });
        mortgageType_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle b = new Bundle();
                b.putSerializable("ls", new String[]{
                        "一抵", "二抵", "一抵转单", "二抵转单"
                });
                Intent i = new Intent(ImmediatelyActivity.this, SelectListActivity.class);
                i.putExtras(b);
                i.putExtra("equalStr", o);
                startActivityForResult(i, REQUEST_CODE_02);
            }
        });
        fourMortgageAmount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle b = new Bundle();
                b.putSerializable("ls", new String[]{
                        "未婚", "已婚", "再婚", "离异", "丧偶"
                });
                Intent i = new Intent(ImmediatelyActivity.this, SelectListActivity.class);
                i.putExtras(b);
                i.putExtra("equalStr", w);
                startActivityForResult(i, REQUEST_CODE_04);
            }
        });
        sevenMortgageAmount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle b = new Bundle();
                b.putSerializable("ls", new String[]{
                        "未婚", "已婚", "再婚", "离异", "丧偶"
                });
                Intent i = new Intent(ImmediatelyActivity.this, SelectListActivity.class);
                i.putExtras(b);
                i.putExtra("equalStr", e);
                startActivityForResult(i, REQUEST_CODE_05);
            }
        });
        firstMortgageAmountProperty_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle b = new Bundle();

                b.putSerializable("ls", new String[]{
                        "银行类", "非银行类"
                });
                Intent i = new Intent(ImmediatelyActivity.this, SelectListActivity.class);
                i.putExtras(b);
                i.putExtra("equalStr", p);
                startActivityForResult(i, REQUEST_CODE_03);
            }
        });

        immediately_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                boolean first = firstMortgageAmount_ll.getVisibility() == View.VISIBLE ? firstMortgageAmount.getText().toString().equals("") : false;
                boolean second = secondMortgageAmount_ll.getVisibility() == View.VISIBLE ? secondMortgageAmount.getText().toString().equals("") : false;
                if (immediate_pensonal_tv.getText().toString().equals("") || requestedAmount.getText().toString().equals("")
                        || loanDuration.getText().toString().equals("") || mortgageType.getText().toString().equals("")
                        || first
                        || second) {
                    LoanToast.showToastShort("信息填写不完整", ImmediatelyActivity.this);
                } else {
                    if (fullName_et.getText().toString().equals("")) {
                        LoanToast.showToastShort("借款人姓名没填", ImmediatelyActivity.this);
                        return;
                    }
                    if (!TextUtils.isEmpty(immediately_cellphone_et.getText().toString()) && !Utils.isMobileNO(immediately_cellphone_et.getText().toString())) {
                        //借款人手机号
                        LoanToast.showToastShort("借款人手机号格式有误", ImmediatelyActivity.this);
                        return;
                    }
                    hashMap = new HashMap();
                    hashMap.put("serialNumber", serialNumber);
                    hashMap.put("requestedAmount", requestedAmount.getText().toString());
                    hashMap.put("loanDuration", loanDuration.getText().toString());
                    hashMap.put("mortgageType", mortgageType.getText().toString());

                    if (firstMortgageAmount.getVisibility() == View.VISIBLE) {
                        hashMap.put("firstMortgageAmount", firstMortgageAmount.getText().toString());
                    }
                    hashMap.put("typeOfFirstMortgage", firstMortgageAmountProperty_tv.getText().toString());
                    if (secondMortgageAmount.getVisibility() == View.VISIBLE) {
                        hashMap.put("secondMortgageAmount", secondMortgageAmount.getText().toString());
                    }

                    hashMap.put("cellphone", immediately_cellphone_et.getText().toString());//借款人手机号
                    hashMap.put("fullName", fullName_et.getText().toString());
                    hashMap.put("maritalStatus", fourMortgageAmount_tv.getText().toString());//借款人的婚姻状态

                    if (sevenMortgageAmount_tv.getVisibility() == View.VISIBLE) {
                        hashMap.put("mortgageStatus", sevenMortgageAmount_tv.getText().toString());//抵押人
                    }

                    op.put("opportunity", hashMap);
                    op.put("sessionId", LoanAccessTokenIdKeeper.readAccessToken(ImmediatelyActivity.this).getAppSessionId());

                    RequestMethod.getMethod().appProjectTime(LoanAccessTokenIdKeeper.readAccessToken(ImmediatelyActivity.this).getAppSessionId(), requestedAmount.getText().toString(),fullName_et.getText().toString(),loanDuration.getText().toString(), ImmediatelyActivity.this, ImmediatelyActivity.this);//这里调用接口请求网络

                }
            }
        });
    }
    @Override
    public void requestSuccess(String tag, String message) {
        if (tag.equals(RequestTag.APPPROJECTTIME)) { //使用tag来辨别消息分发

            Intent intent1 = new Intent(ImmediatelyActivity.this, UpLoadMaterialActivity.class);
            intent1.putExtra("starus1", immediate_pensonal_tv.getText().toString());
            intent1.putExtra("gam", fourMortgageAmount_tv.getText().toString());
            intent1.putExtra("gam1", sevenMortgageAmount_tv.getText().toString());
            intent1.putExtra("serialnumber", serialNumber);
            intent1.putExtra("isHome", isHome);
            OP opobj = new OP();
            opobj.setHm(hashMap);
            intent1.putExtra("submitparms",opobj);
//            intent1.putExtra("submitparms", gson.toJson(op));

            startActivity(intent1);

        }
    }

    @Override
    public void requestError(String message) {
        LoanLog.e("cus", message);
        FailureResultObject failureResultObject = new FailureResultObject();
        failureResultObject = gson.fromJson(message, FailureResultObject.class);
        LoanToast.showToastShort(failureResultObject.getErrorMessage(), ImmediatelyActivity.this);
    }
}
